package com.rh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rh.entity.EduComment;
import com.rh.entity.vo.UcenterMemberVo;
import com.rh.exception.EducationException;
import com.rh.mapper.EduCommentMapper;
import com.rh.service.EduCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rh.service.UcenterMemberService;
import com.rh.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 评论 服务实现类
 * @author rh
 */
@Service
@Transactional
@Slf4j
public class EduCommentServiceImpl extends ServiceImpl<EduCommentMapper, EduComment> implements EduCommentService {

    @Autowired
    private UcenterMemberService ucenterMemberService;
    /**
     * 根据课程id分页查询用户信息
     * @param page
     * @param limit
     * @param courseId
     * @return
     */
    @Override
    public Map<String, Object> getCommentPageList(long page, long limit, String courseId) {
        Page<EduComment> commentPage=new Page<>(page,limit);
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        //判断courseId是否为空
        if (!StringUtils.isEmpty(courseId)){
            wrapper.eq("course_id",courseId);
        }
        //按照最新评论排序
        wrapper.orderByDesc("gmt_create");
        //调用方法分页
        baseMapper.selectPage(commentPage,wrapper);
        //list集合数据
        List<EduComment> records = commentPage.getRecords();
        long current = commentPage.getCurrent();//当前页
        long size = commentPage.getSize();//一页记录数
        long total = commentPage.getTotal();//总记录数
        long pages = commentPage.getPages();//总页数
        boolean hasPrevious = commentPage.hasPrevious();//是否有上页
        boolean hasNext = commentPage.hasNext();//是否有下页

        Map<String, Object> map = new HashMap<>();
        map.put("items",records);
        map.put("current",current);
        map.put("pages",pages);
        map.put("size",size);
        map.put("total",total);
        map.put("hasPrevious",hasPrevious);
        map.put("hasNext",hasNext);
        return map;
    }

    /**
     * 添加评论
     * @param request
     * @param eduComment
     * @return
     */
    @Override
    public int addComment(HttpServletRequest request, EduComment eduComment) {
        //获取memberId
        String memberId= JwtUtils.getMemberIdByJwtToken(request);
        //判断用户是否登陆
        if(StringUtils.isEmpty(memberId)){
            throw new EducationException(20001,"请先登陆!");
        }
        //设置memberid
        eduComment.setMemberId(memberId);
        //调用ucenterMemberService获取用户信息
        UcenterMemberVo ucenterMemberVo = ucenterMemberService.getMemberInfoById(memberId);
        eduComment.setAvatar(ucenterMemberVo.getAvatar());
        eduComment.setNickname(ucenterMemberVo.getNickname());
        int insert = baseMapper.insert(eduComment);
        return insert;
    }
}
